# Dinghong Cloud Screen Project Cleanup Script (Windows PowerShell)
# For cleaning up Minikube deployment

$ErrorActionPreference = "Stop"

Write-Host "Starting to cleanup Dinghong Cloud Screen project..." -ForegroundColor Yellow

# Get script directory
$SCRIPT_DIR = Split-Path -Parent $MyInvocation.MyCommand.Path

# Delete Kubernetes resources
Write-Host "Deleting Kubernetes resources..." -ForegroundColor Yellow
kubectl delete -f "$SCRIPT_DIR\nginx-ingress.yaml" --ignore-not-found=true
kubectl delete -f "$SCRIPT_DIR\web-deployment.yaml" --ignore-not-found=true
kubectl delete -f "$SCRIPT_DIR\nginx-config.yaml" --ignore-not-found=true
kubectl delete -f "$SCRIPT_DIR\server-deployment.yaml" --ignore-not-found=true
kubectl delete -f "$SCRIPT_DIR\configmap-server.yaml" --ignore-not-found=true
kubectl delete -f "$SCRIPT_DIR\redis-deployment.yaml" --ignore-not-found=true
kubectl delete -f "$SCRIPT_DIR\mysql-deployment.yaml" --ignore-not-found=true
kubectl delete -f "$SCRIPT_DIR\namespace.yaml" --ignore-not-found=true

# Wait for resource deletion to complete
Write-Host "Waiting for resource deletion to complete..." -ForegroundColor Yellow
Start-Sleep -Seconds 10

# Delete local Docker images
Write-Host "Deleting local Docker images..." -ForegroundColor Yellow
docker rmi dinghong-cloud-screen-server:latest --force 2>$null
docker rmi dinghong-cloud-screen-web:latest --force 2>$null

# Delete images from Minikube
Write-Host "Deleting images from Minikube..." -ForegroundColor Yellow
minikube image rm dinghong-cloud-screen-server:latest 2>$null
minikube image rm dinghong-cloud-screen-web:latest 2>$null

# Clean up hosts file records
Write-Host "Cleaning up hosts file records..." -ForegroundColor Yellow
$hostsPath = "$env:SystemRoot\System32\drivers\etc\hosts"
$hostsContent = Get-Content $hostsPath -Raw
if ($hostsContent -match "dinghong-cloud-screen\.local") {
    $newContent = $hostsContent -replace "`r?`n.*dinghong-cloud-screen\.local.*`r?`n?", "`n"
    Set-Content -Path $hostsPath -Value $newContent -Force
    Write-Host "Cleaned up hosts record" -ForegroundColor Green
}

Write-Host "Cleanup completed!" -ForegroundColor Green 